package zut.edu.cs.network.threads;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.*;

public class BigFileApplication { 
	BigFileApplication application = new BigFileApplication();
		
	public static void main(String[] args) {
		String[] arg1 = {"E:\\书本信息.txt"};
		
	try { 
			executor(arg1[0]); 
		} catch (Exception e) { 
			e.printStackTrace(); 
			LOG.error("执行器错误. {}", e.getMessage()); 
		} 
	} 
	private static void executor(String inputFile) throws Exception {
//		阻塞队列
		BlockingQueue<List<String>> blockingQueue = new ArrayBlockingQueue<>(DEFAULT_QUEUE_SIZE); 
//		创建线程池
		ThreadPoolExecutor pools = getThreadPool();   
		pools.execute(new InnerReader(blockingQueue, inputFile, DEFAULT_QUEUE_SIZE));
		while (true) { 
			if (blockingQueue.isEmpty()) { 
				TimeUnit.SECONDS.sleep(1); 
			} 
			List<String> dataGroup = blockingQueue.peek(); 
			if (dataGroup != null && dataGroup.isEmpty()) {
				LOG.info("process the big file done."); 
				break; 
			}
			pools.execute(new InnerWriter(blockingQueue)); 
		} 
		pools.shutdown();
	} 
	
	private static ThreadPoolExecutor getThreadPool() { 
		return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, 
				new LinkedBlockingDeque<>(10), new ThreadPoolExecutor.CallerRunsPolicy()); 
		
	} 
	
	private static final int DEFAULT_QUEUE_SIZE = 30; 
	private static final Logger LOG = LoggerFactory.getLogger(BigFileApplication.class); 
	
}
	
	
	
	
	
	
